package utils;

import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class ImageConverter {
	
	// src = http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/

	public static String toString(Image img) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageIO.write((RenderedImage) img, "jpg", baos);
		baos.flush();
		byte[] imageInByte = baos.toByteArray();
		baos.close();
		
		String retour = "";
		for (byte b : imageInByte)
			retour += b + "-";
		return retour.substring(0, retour.length() - 1);
	}
	
	public static Image toImage(String str) throws IOException {
		byte[] imageInByte = str.getBytes();
		
		InputStream in = new ByteArrayInputStream(imageInByte);
		return ImageIO.read(in);
	}
}
